﻿using System;
using ECommerce.IntegrationSpecs;
using ECommerce.IntegrationSpecs.TestDataBuilders;
using ECommerce.Web.Models;
using ECommerce.Web.ViewModels;
using Machine.Specifications;
using NHibernate;

namespace Web_product_controller_specs
{
    [Subject("Product information")]
    public class When_a_visitor_views_a_product : WebContext
    {
        static Product product;

        Establish context = () =>
            {
                using (ITransaction transaction = Session.BeginTransaction())
                {
                    product = new ProductBuilder().Build();

                    Session.Save(product);

                    transaction.Commit();
                }
            };

        Because of = () =>
            {
                Browser.Open(String.Format("Product/Show/{0}", product.Id));
            };

        It should_display_product_informantion = () =>
            {
                ViewFor<ProductShowView>()
                    .ShouldDisplayText(x => x.Name, product.Name)
                    .ShouldDisplayText(x => x.Price, product.Price.ToString("c"));
            };
    }

    [Subject("Product information")]
    public class When_a_visitor_attempts_to_view_a_product_that_cannot_be_found : WebContext
    {
        Because of = () =>
            {
                Browser.Open("Product/Show/-1");
            };

        It should_display_message_to_explain_the_product_is_not_available = () =>
            {
                Browser.IsTextPresent("Product Not Found").ShouldBeTrue();
            };
    }    
}
